<? ob_start(); ?>
<?php
class Category extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('category_model');
        $this->load->helper('main_helper');
        $this->data['cur_cat'] = 'current';
    }
    function index()
    {
        $this->data['categories']=$this->category_model->getall();
        $this->data['all']='current2';
        $this->date['cur_cat'] = 'current';
        //echo '<pre>'; var_dump($data['cate']); echo '</pre>';die;
        $this->load->view('backend/header',$this->data);
        $this->load->view('backend/category/home',$this->data);
        $this->load->view('backend/footer');
    }
    function insert()
    {
        if($post=$this->input->post())
        {
            $this->load->library('form_validation');
            $this->form_validation->set_rules($this->category_model->getRules());
            if($this->form_validation->run() === true )
            {
                $this->category_model->save($post);
                $this->session->set_flashdata('insert','ban da them moi thanh cong 1 category voi ten la '.$post['category_name']);
                redirect('admin/category');
            }
        }
        $this->data['insert']='current2';
        $this->load->view('backend/header',$this->data);
        $this->load->view('backend/category/form');
        $this->load->view('backend/footer');

    }
    function update($category_id)
    {
        if($post=$this->input->post())
        {
            $this->load->library('form_validation');
            $this->form_validation->set_rules($this->category_model->getRules());
            if($this->form_validation->run()=== true )
            {
                $this->category_model->save($post,$category_id);
                $this->session->set_flashdata('edit','ban da sua thanh cong 1 category co ten la '.$post['category_name']);
                redirect('admin/category');
            }
        }
        $data['category']=$this->category_model->getone($category_id);

        $this->load->view('backend/category/form',$data);
    }
    function delete($category_id)
    {
        //muon xoa category thi truoc tien phai xoa tat ca cac san phan thuoc category,sau do xoa category
        $this->category_model->delete($category_id);
        redirect('admin/category');
    }
    function updateStatus($category_id = 0 , $category_active = 0)
    {
        $category_id 	= intval($category_id);
        $category_active = intval($category_active);

        $this->category_model->update_item_status( $category_id, $category_active );

    }
    
}